const express = require('express');
const router = express.Router();
const AdminTodo = require('../mysql/collection/AdminTodo');

/**
 * @api {get} /admin/todo/list 获取待办事项列表
 * @apiName GetTodoList
 * @apiGroup AdminTodo
 * @apiHeader {String} token token
 * @apiParam {Number} [pageSize=10] 每页显示数量
 * @apiParam {Number} [pageNum=1] 当前页码
 * @apiParam {Number} [status] 状态筛选
 * @apiParam {Number} [priority] 优先级筛选
 * @apiSuccess {Object} data 分页数据
 */
router.get('/list', async (req, res) => {
  try {
    const { pageSize = 10, pageNum = 1, status, priority } = req.query;
    const skip = (pageNum - 1) * pageSize;
    
    const query = { flag: true };
    console.log(req.user);
    
    if( req?.user?.id){
      console.log("req?.user?.id", req?.user?.id);
      
      query.creatorId = req.user.id;
    }
    if (status !== undefined) query.status = parseInt(status);
    if (priority !== undefined) query.priority = parseInt(priority);
    
    const [todos, total] = await Promise.all([
      AdminTodo.find(query).skip(skip).limit(parseInt(pageSize)),
      AdminTodo.countDocuments(query)
    ]);
    
    res.json({ 
      code: 200, 
      data: {
        list: todos,
        total,
        pageSize: parseInt(pageSize),
        pageNum: parseInt(pageNum)
      }
    });
  } catch (err) {
    res.status(500).json({ code: 500, message: err.message });
  }
});

/**
 * @api {post} /admin/todo/create 创建待办事项
 * @apiName CreateTodo
 * @apiGroup AdminTodo
 * @apiHeader {String} token token
 * @apiParam {String} title 标题
 * @apiParam {String} content 内容
 * @apiParam {Number} [priority=2] 优先级
 * @apiParam {String} creatorId 创建者ID
 */
router.post('/create', async (req, res) => {
  try {
    const { title, content, priority = 2, creatorId } = req.body;
    if (!title || !content || !creatorId) {
      return res.status(400).json({ code: 400, message: '标题、内容和创建者ID不能为空' });
    }
    
    const todo = new AdminTodo({
      // todoId: Date.now().toString(),
      title,
      content,
      priority: parseInt(priority),
      status: 0,
      creatorId,
      flag: true
    });
    
    await todo.save();
    res.json({ code: 200, data: todo });
  } catch (err) {
    res.status(500).json({ code: 500, message: err.message });
  }
});

/**
 * @api {put} /admin/todo/update/:id 更新待办事项
 * @apiName UpdateTodo
 * @apiGroup AdminTodo
 * @apiHeader {String} token token
 * @apiParam {String} id 待办ID
 * @apiParam {String} [title] 标题
 * @apiParam {String} [content] 内容
 * @apiParam {Number} [priority] 优先级
 * @apiParam {Number} [status] 状态
 * @apiParam {String} [assigneeId] 负责人ID
 * @apiParam {Date} [dueDate] 截止日期
 */
router.put('/update/:id', async (req, res) => {
  try {
    const updateFields = {};
    if (req.body.title) updateFields.title = req.body.title;
    if (req.body.content) updateFields.content = req.body.content;
    if (req.body.priority) updateFields.priority = parseInt(req.body.priority);
    if (req.body.status) updateFields.status = parseInt(req.body.status);
    if (req.body.assigneeId) updateFields.assigneeId = req.body.assigneeId;
    if (req.body.dueDate) updateFields.dueDate = req.body.dueDate;
    updateFields.updateTime = Date.now();
    
    const todo = await AdminTodo.findByIdAndUpdate(
      req.params.id,
      updateFields,
      { new: true }
    );
    
    if (!todo) {
      return res.status(404).json({ code: 404, message: '待办事项不存在' });
    }
    
    res.json({ code: 200, data: todo });
  } catch (err) {
    res.status(500).json({ code: 500, message: err.message });
  }
});

/**
 * @api {delete} /admin/todo/delete/:id 删除待办事项
 * @apiName DeleteTodo
 * @apiGroup AdminTodo
 * @apiHeader {String} token token
 * @apiParam {String} id 待办ID
 */
router.delete('/delete/:id', async (req, res) => {
  try {
    const todo = await AdminTodo.findByIdAndUpdate(
      req.params.id,
      { flag: false, updateTime: Date.now() },
      { new: true }
    );
    
    if (!todo) {
      return res.status(404).json({ code: 404, message: '待办事项不存在' });
    }
    
    res.json({ code: 200, data: todo });
  } catch (err) {
    res.status(500).json({ code: 500, message: err.message });
  }
});

module.exports = router;